package com.dqp.cslggroup.yunPrint;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dqp.cslggroup.EventBus.MessageEvent;
import com.dqp.cslggroup.R;
import com.dqp.cslggroup.UI.BaseActivity;
import com.dqp.cslggroup.Util.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.IOException;
import java.io.Serializable;
import java.util.Map;

public class confirmOrder extends BaseActivity {
    TextView copiesV, nameV, snoV, phoneV, typeV, modeV, priceV, fileNameV, pageV, totalV, roomV, outV, submitV;
    JSONObject jsonObject;
    Button submit;
    String url = "http://210.28.164.4:9999/printorder/yunprint/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.confirmorder);
        EventBus.getDefault().register(this);
        initCmp();
        Intent intent = getIntent();
        String jsonObjectString = intent.getStringExtra("json");
        jsonObject = JSON.parseObject(jsonObjectString);
        setData();
    }

    public void initCmp() {
        snoV = findViewById(R.id.sno);
        phoneV = findViewById(R.id.phone);
        typeV = findViewById(R.id.type);
        modeV = findViewById(R.id.mode);
        priceV = findViewById(R.id.price);
        pageV = findViewById(R.id.page);
        totalV = findViewById(R.id.total);
//        roomV = findViewById(R.id.room);
        outV = findViewById(R.id.outtradeno);
        fileNameV = findViewById(R.id.fileName);
        submitV = findViewById(R.id.submitTime);
        nameV = findViewById(R.id.name);
        copiesV = findViewById(R.id.copies);
        roomV = findViewById(R.id.dormitory);
        nameV = findViewById(R.id.name);
        submit = findViewById(R.id.submit);
        submit.setOnClickListener(view -> {
            new Thread(this::submit).start();
        });
    }

    public void setData() {
        if (jsonObject != null) {
            snoV.append(jsonObject.getString("sno"));
            phoneV.append(jsonObject.getString("phone"));
            typeV.append(jsonObject.getString("type"));
            modeV.append(jsonObject.getString("mode"));
            priceV.append(jsonObject.getString("price"));
            pageV.append(jsonObject.getString("page"));
            totalV.append(jsonObject.getString("total"));
            roomV.append(jsonObject.getString("dormitory"));
            outV.append(jsonObject.getString("outtradeno"));
            fileNameV.append(jsonObject.getString("fileName"));
            submitV.append(jsonObject.getString("submitTime"));
            nameV.append(jsonObject.getString("name"));
            copiesV.append(jsonObject.getString("copies"));
        }

    }


    public void submit() {
        Connection.Response response = null;
        try {
            response = Jsoup.connect(url + "/submitPrintTask/outtradeno=" + jsonObject.getString("outtradeno"))
                    .ignoreContentType(true)
                    .method(Connection.Method.POST)
                    .timeout(5000)
                    .execute();
            System.out.println(response.body());
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        JSONObject jsonObject = JSON.parseObject(response.body());
        Log.e("js", jsonObject.toJSONString());
        if (jsonObject != null && jsonObject.containsKey("result") && "success".equals(jsonObject.getString("result"))) {
            EventBus.getDefault().post(new MessageEvent("confirmOrder", 0, jsonObject.getString("resultMsg")));
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        if (event.getEvent().equals("confirmOrder")) {
            if (event.getWhat() == 0) {
                ToastUtil.showToast(event.getMessage());

            }

        }

    }
}